package com.cgbl.cg.view

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.core.BottomPopupView
import com.cgbl.cg.R
import com.cgbl.cg.adapter.CityAdapter
import com.cgbl.cg.adapter.JobAdapter
import com.cgbl.cg.utils.city.CityUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import me.goldze.mvvmhabit.utils.ToastUtils

class UpdateCityPop(context: Context) : BottomPopupView(context) {

    lateinit var jobList: RecyclerView

    lateinit var backView: ImageView

    lateinit var titleView: TextView

    lateinit var jobAdapter: CityAdapter

    var pageType = PAGE_TYPE_PROVINCE

    var provincePos = -1;

    var cityPos = -1;

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_update_city
    }

    override fun initPopupContent() {
        super.initPopupContent()
        val provinces = CityUtils.getProvince()
        jobList = findViewById(R.id.recyclerView)
        backView = findViewById(R.id.iv_back)
        titleView = findViewById(R.id.titleView)
        findViewById<View>(R.id.iv_close).setOnClickListener {
            dismiss()
            initList()
        }
        jobAdapter = CityAdapter()
        jobList.layoutManager = LinearLayoutManager(context)
        jobList.adapter = jobAdapter

        backView.setOnClickListener {
            if (pageType == PAGE_TYPE_AREA) {
                val citys = provinces[provincePos].cityList
                val cityNameList = citys.map {
                    it.name
                }
                jobAdapter.setNewInstance(ArrayList(cityNameList))
                backView.visibility = View.VISIBLE
                titleView.text = provinces[provincePos].name
                pageType = PAGE_TYPE_CITY
                jobAdapter.setIsVisibleRightIcon(true)
                return@setOnClickListener
            }

            if (pageType == PAGE_TYPE_CITY) {
                initList()
                return@setOnClickListener
            }
        }

        jobAdapter.setOnItemClickListener { adapter, view, position ->
            if (pageType == PAGE_TYPE_PROVINCE) {
                //当前列表是一级
                provincePos = position
                val citys = provinces[provincePos].cityList
                val cityNameList = citys.map {
                    it.name
                }
                jobAdapter.setNewInstance(ArrayList(cityNameList))
                backView.visibility = View.VISIBLE
                titleView.text = provinces[provincePos].name
                pageType = PAGE_TYPE_CITY
                jobAdapter.setIsVisibleRightIcon(true)
                return@setOnItemClickListener
            }

            if (pageType == PAGE_TYPE_CITY) {
                //当前列表是一级
                cityPos = position
                val areas = provinces[provincePos].cityList[cityPos].area
                jobAdapter.setNewInstance(ArrayList(areas))
                backView.visibility = View.VISIBLE
                titleView.text = provinces[provincePos].cityList[cityPos].name
                pageType = PAGE_TYPE_AREA
                jobAdapter.setIsVisibleRightIcon(false)
                return@setOnItemClickListener
            }

            if (pageType == PAGE_TYPE_AREA) {
                //当前列表是一级
                val area = provinces[provincePos].cityList[cityPos].area[position]
                ToastUtils.showShort("选择城市-$area")
                dismiss()
                initList()
                return@setOnItemClickListener
            }
        }
        val provinceNameList = provinces.map {
            it.name
        }
        jobAdapter.setNewInstance(ArrayList(provinceNameList))
    }

    private fun initList() {
        val provinces = CityUtils.getProvince()
        pageType = PAGE_TYPE_PROVINCE
        cityPos = -1
        provincePos = -1
        backView.visibility = View.GONE
        titleView.text = "选择城市"
        val provinceNameList = provinces.map {
            it.name
        }
        jobAdapter.setNewInstance(ArrayList(provinceNameList))
        jobAdapter.setIsVisibleRightIcon(true)
    }

    companion object {

        const val PAGE_TYPE_PROVINCE = 1

        const val PAGE_TYPE_CITY = 2

        const val PAGE_TYPE_AREA = 3

    }
}